package com.xuxin.baby.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.LoginGuideChildAdapter;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.UserChildBean;
import com.xuxin.baby.contract.UserChildContract;
import com.xuxin.baby.glide.GlideApp;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.presenter.UserChildPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020/11/23 0023
 * 登录引导
 * 第三步添加宝宝
 */
public class LoginGuide1Activity extends BaseActivity implements UserChildContract.View {
    private final UserChildContract.Presenter presenter = new UserChildPresenter(this);
    RequestOptions options = RequestOptions.bitmapTransform(new CircleCrop());
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.view_line)
    ImageView view_line;
    @BindView(R.id.login_guide_image)
    ImageView login_guide_image;
    @BindView(R.id.login_guide_name)
    TextView login_guide_name;
    @BindView(R.id.login_guide_group)
    RadioGroup login_guide_group;
    @BindView(R.id.login_guide_recycle)
    RecyclerView login_guide_recycle;
    @BindView(R.id.login_guide_next)
    Button login_guide_next;

    private LoginGuideChildAdapter myAdapter;
    private ACache aCache;
    private String memberId;//用户id
    private String tenantId;//租户id
    private String token;//授权token

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_login_guide1);
    }

    @Override
    public void initView() {
        title_name.setText("添加宝宝");
        title_left.setVisibility(View.INVISIBLE);
        view_line.setVisibility(View.INVISIBLE);
        myAdapter = new LoginGuideChildAdapter();
        RecycleUtils.init(login_guide_recycle);
        login_guide_recycle.setAdapter(myAdapter);
        View footer = View.inflate(context, R.layout.item_user_child_footer, null);
        myAdapter.addFooterView(footer);
        footer.findViewById(R.id.item_user_child_add).setOnClickListener(this);
    }

    @Override
    public void initData() {
        aCache = ACache.get(context);
        memberId = aCache.getAsString(BaseConstant.memberId);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        String role = getIntent().getStringExtra("role");
        String roleName = getIntent().getStringExtra("roleName");
        String roleImage = Retrofit.image_url +  getIntent().getStringExtra("roleImage");
        login_guide_name.setText(roleName);
        if ("爸爸".equals(role)) {
            login_guide_group.check(R.id.login_guide_dad);
            GlideApp.with(context).load(roleImage).placeholder(R.mipmap.img_login_parent_father).apply(options).into(login_guide_image);
        } else {
            login_guide_group.check(R.id.login_guide_mom);
            GlideApp.with(context).load(roleImage).placeholder(R.mipmap.img_login_parent_mother).apply(options).into(login_guide_image);
        }
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.login_guide_next})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.login_guide_next) {
            if (myAdapter.getData().size() > 0) {
                aCache.put(BaseConstant.login, true);
                ActivityUtils.startActivity(MainActivity.class);
                finish();
            } else {
                ToastUtils.showShort("请添加宝宝");
            }
        } else if (id == R.id.item_user_child_add) {
            ActivityUtils.startActivityForResult(this, UserChildDataActivity.class, 200);
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getUserChild(tenantId, token, memberId);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(UserChildBean userChildBean) {
        myAdapter.setList(userChildBean.getResult().getItems());
        if (null != userChildBean.getResult()
                && null != userChildBean.getResult().getItems()
                && userChildBean.getResult().getItems().size() > 0) {
            login_guide_next.setEnabled(true);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200 && resultCode == 200) {
            initData();
        }
    }
}